package com.hqhsc.module.manage.channel.dao;

import com.hqhsc.core.dao.BaseJdbcDaoImpl;
import com.hqhsc.module.manage.channel.domain.Channel;
import com.hqhsc.module.manage.channel.domain.ChannelRowMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class ChannelManageDao extends BaseJdbcDaoImpl {
	final Logger logger = LoggerFactory.getLogger(ChannelManageDao.class);

    public long getChannelCount(long org_id , String channel_name , int channel_type) {
        String sql = "select count(1) from channel t where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if(StringUtils.isNotEmpty(channel_name)) {
            sql = sql + " and name like ? ";
            list.add("%"+channel_name+"%");
        }
        if(channel_type != -1) {
            sql = sql + " and channel_type = ? ";
            list.add(channel_type);
        }
        Object[] o = list.toArray();
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForLong(sql, o);
    }

    public List<Channel> getChannelList(long org_id , String channel_name , int channel_type , String orderby , String orderby_type , int start ,  int size) {

        String sql = "select * from channel t where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if(StringUtils.isNotEmpty(channel_name)) {
            sql = sql + " and name like ? ";
            list.add("%"+channel_name+"%");
        }
        if(channel_type != -1) {//-1表示查所有；0表示线下；1表示线上
            sql = sql + " and channel_type = ? ";
            list.add(channel_type);
        }
        if(StringUtils.isNotEmpty(orderby)) {
            sql = sql + " order by " + orderby + " " + orderby_type;
        } else {
            sql = sql + " order by id desc ";
        }
        sql = sql + " limit ? ,? ";
        list.add(start);
        list.add(size);
        Object[] o = list.toArray();
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().query(sql, o, new ChannelRowMapper());
    }


    public int insertChannel(String channel_name , int channel_type , String website_url , String spread_position , String spread_cycle, String spread_content, int spread_cost , long org_id , long user_id) {
        String sql = "insert into channel (name , channel_type , website_url , spread_position , spread_cycle , spread_content , spread_cost , org_id , create_by) values (?,?,?,?,?,?,?,?,?)";
        Object[] o = new Object[] { channel_name , channel_type , website_url , spread_position , spread_cycle , spread_content , spread_cost , org_id , user_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql, o);
    }

    public int updateChannel(long channel_id , String channel_name ,int channel_type, String spread_position, String spread_cycle, String spread_content, int spread_cost ,  long org_id) {
        String sql = "update channel set name = ? , channel_type = ? , spread_position = ? , spread_cycle = ? , " +
                "spread_content = ? , spread_cost = ? where id = ? ";
        Object[] o = new Object[] { channel_name , channel_type , spread_position , spread_cycle , spread_content , spread_cost , channel_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql, o);
    }

    /**
     * 根据channel_id，查询该渠道下的活动名称
     *
     * @param channel_id
     * @return
     */
    public List<String> getActivityNameByChannelId(long channel_id) {
        try{
            String sql = "select b.name from channel a , activity b , activity_channel_detail c where a.id = c.channel_id and b.id = c.activity_id and a.id = ?";
            Object[] o = new Object[] { channel_id };
            logger.info(sql.replaceAll("\\?", "{}"), o);
            return this.getJdbcTemplate().queryForList(sql, o, String.class);
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
            return new ArrayList<String>();
        }
    }





}
